package cn.wegoteam.shop.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import cn.wegoteam.shop.inter.AddressServiceInter;
import cn.wegoteam.shop.po.Address;
import cn.wegoteam.shop.po.User;
import cn.wegoteam.shop.util.Const;
import cn.wegoteam.shop.util.DataUtils;
import cn.wegoteam.shop.util.MyString;

@ParentPackage("users")
@Namespace("/")
@Controller
public class AddressInteceptorAction extends BaseAction<Address> {
	@Resource(name="addressService")
	private AddressServiceInter addressService;
	private String defaultAddress;
	@Action(value = "userAddressInfo", results = { @Result(name = SUCCESS, location = SUBPAGES
			+ "addressInfo.jsp") })
	public String userAddressInfo() {
		models = addressService.getList(null, DataUtils.getMap("user.id",getSessionUser().getId()));
		return SUCCESS;
	}
	@Action(value="saveOrUpdateAddr",results={@Result(name=SUCCESS,location=SUBPAGES+"addressInfo.jsp")})
	public String saveOrUpdateAddr(){
		if(model.getId()!=null&&!"".equals(model.getId())){
			if(defaultAddress!=null){
				addressService.toggleDefStatus(getSessionUser());
				model.setDef("2");
			}
			addressService.updateAddrBySql(model);
			model=null;
		}else{
			if(defaultAddress!=null){
				addressService.toggleDefStatus(getSessionUser());
				model.setDef("2");
			}
			model.setUser(getSessionUser());
			addressService.save(model);
			model=null;
		}
		models = addressService.getList(null, DataUtils.getMap("user.id",getSessionUser().getId()));
		return SUCCESS;
	}
	@Action(value="findAddressById",results={@Result(name=SUCCESS,location=SUBPAGES+"addressInfo.jsp")})
	public String findAddressById(){
		model=addressService.get(model.getId());
		models = addressService.getList(null, DataUtils.getMap("user.id",getSessionUser().getId()));
		return SUCCESS;
	}
	@Action(value="deleteAddressById",results={@Result(name=SUCCESS,location=SUBPAGES+"addressInfo.jsp")})
	public String deleteAddressById(){
		addressService.delete(model.getId());
		models = addressService.getList(null, DataUtils.getMap("user.id",getSessionUser().getId()));
		return SUCCESS;
	}
	@Action(value="setAddressDefault",results={@Result(name=SUCCESS,location=SUBPAGES+"addressInfo.jsp")})
	public String setAddressDefault(){
		addressService.toggleStatus(model,getSessionUser());
		model=null;
		models = addressService.getList(null, DataUtils.getMap("user.id",getSessionUser().getId()));
		return SUCCESS;
	}
	public String getDefaultAddress() {
		return defaultAddress;
	}
	public void setDefaultAddress(String defaultAddress) {
		this.defaultAddress = defaultAddress;
	}
}
